#include "iniwidget.h"
#include "ui_iniwidget.h"

IniWidget::IniWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::IniWidget)
{
    ui->setupUi(this);
}

IniWidget::~IniWidget()
{
    delete ui;
}

void IniWidget::on_pushButton_clicked()
{
    QString path = "D:/dtemp/my.ini";
    readIniFile(path);
}




// 读取INI文件
void IniWidget::readIniFile(const QString &filePath) {
    QSettings settings(filePath, QSettings::IniFormat);

    // 读取某个入口
    QString value = settings.value("mysqld/datadir").toString();

    // 遍历所有的section和key
    QStringList groups = settings.childGroups();
    foreach (const QString &group, groups) {
        settings.beginGroup(group);
        QStringList keys = settings.childKeys();
        foreach (const QString &key, keys) {
            QString data = settings.value(key).toString();
            qDebug() << group << "/" << key << ": " << data;
        }
        settings.endGroup();
    }
}

// 修改INI文件
void IniWidget::writeToIniFile(const QString &filePath) {
    QSettings settings(filePath, QSettings::IniFormat);

    // 写入某个入口
    settings.setValue("section/key", "new value");
}
